<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <div slot="title" class="title-text">我的频道</div>
      <van-button
        class="edit-btn"
        type="danger"
        plain
        round
        size="mini"
        @click="isEditShow = !isEditShow"
        >{{ isEditShow ? "完成" : "编辑" }}</van-button
      >
    </van-cell>
    <van-grid class="my-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        :key="index"
        v-for="(item, index) in userChannels"
        @click="onUserChannelClick(item, index)"
      >
        <van-icon
          slot="icon"
          name="clear"
          v-if="isEditShow && item.name !== '推荐'"
        ></van-icon>
        <span class="text"
        :class="{ active: activeIndex===index }"
         slot="text">{{ item.name }}</span>
      </van-grid-item>
    </van-grid>

    <!-- 频道推荐 -->
    <van-cell :border="false">
      <div slot="title" class="title-text">频道推荐</div>
    </van-cell>
    <van-grid class="recommend-grid" :gutter="10">
      <van-grid-item
        class="grid-item"
        icon="plus"
        :key="index"
        v-for="(item, index) in recommendChannels"
        :text="item.name"
        @click="onAddChannel(item)"
      />
    </van-grid>
    <!-- /频道推荐 -->
  </div>
</template>

<script>
import {
getAllChannels,
addUserChannel,
deleteUserChannel } from "@/api/channel";

import { setItem } from '@/utils/storage'
export default {
  name: "ChannelEdit",
  components: {},
  props: {
    // 父传子
    userChannels: {
      type: Array,
      required: true
    },
    activeIndex: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      allChannels: [], // 所有频道
      isEditShow: false // 是否修改
    };
  },
  computed: {
    // 推荐频道 = 所有频道-我的频道
    recommendChannels() {
      //   let channel = []
      // this.allChannels.forEach(val=>{
      //   let idx = this.userChannels.findIndex(item=>item.id===val.id)
      //   if(idx===-1){
      //     channel.push(val)
      //   }
      // })
      // return channel

      return this.allChannels.filter(item => {
        return (
          this.userChannels.findIndex(userItem => userItem.id === item.id) ===
          -1
        );
      });
    }
  },
  watch: {},
  created() {
    this.loadAllChannels();
  },
  mounted() {},
  methods: {
    // 加载所有频道
    async loadAllChannels() {
      try {
        const { data: res } = await getAllChannels();
        this.allChannels = res.data.channels;
      } catch (error) {
        this.$toast.fail("获取所有频道失败");
      }
    },
    // 添加频道
    async onAddChannel(val) {
      this.userChannels.push(val);
      if(this.$store.state.user){
        try {
          await addUserChannel([{
            id:val.id,
            seq:this.userChannels.length
          }])
        } catch (error) {
          this.$toast.fail('保存失败')
        }
      }else{
        setItem('TOUTIAO_USER_CHANNEL',this.userChannels)
      }
    },
    // 用户频道点击
    async onUserChannelClick(val,idx) {
      if(this.isEditShow){
        if(val.name!=='推荐'){
         if(idx<= this.activeIndex){
           this.$emit('update-index',this.activeIndex-1)
         }
            this.userChannels.splice(idx,1)
            if(this.$store.state.user){
              try {
                await deleteUserChannel(val.id)
              } catch (error) {
                this.$toast.fail('删除失败')
              }
            }else{
              setItem('TOUTIAO_USER_CHANNEL',this.userChannels)
            }
        }
      }else{
        this.$emit('update-index',idx)
        this.$emit('close')
      }
    }
  }
};
</script>

<style scoped lang="less">
.channel-edit {
  padding: 85px 0;

  .title-text {
    font-size: 32px;
    color: #333333;
  }

  .edit-btn {
    width: 104px;
    height: 48px;
    font-size: 26px;
    color: #f85959;
    border: 1px solid #f85959;
  }

  /deep/ .grid-item {
    width: 160px;
    height: 86px;
    .van-grid-item__content {
      white-space: nowrap;
      background-color: #f4f5f6;
      .van-grid-item__text,
      .text {
        font-size: 28px;
        color: #222;
        margin-top: 0;
      }
      .active {
        color: red;
      }
      .van-grid-item__icon-wrapper {
        position: unset;
      }
    }
  }

  /deep/ .my-grid {
    .grid-item {
      .van-icon-clear {
        position: absolute;
        right: -10px;
        top: -10px;
        font-size: 30px;
        color: #cacaca;
        z-index: 2;
      }
    }
  }

  /deep/ .recommend-grid {
    .grid-item {
      .van-grid-item__content {
        flex-direction: row;
        .van-icon-plus {
          font-size: 28px;
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
